package cn.aib.eduservice.controller;


import cn.aib.commonutils.R;
import cn.aib.eduservice.entity.EduChapter;
import cn.aib.eduservice.entity.chapter.ChapterVo;
import cn.aib.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    //课程大纲列表,根据课程id进行查询
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        System.out.println("-------------------:"+list);
        return R.ok().data("allChapterVideo",list);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter){

        //调用service添加章节
        chapterService.save(chapter);

        return R.ok();
    }

    //根据章节ID查询章节
    @GetMapping("getChapter/{chapterId}")
    public R getChapterByChapterId(@PathVariable String chapterId){
        //调用service添加章节
        EduChapter chapter = chapterService.getById(chapterId);

        return R.ok().data("chapter",chapter);
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter){
        //调用service添加章节
        chapterService.updateById(chapter);

        return R.ok();
    }

    //删除章节:由于删除章节涉及一定的业务，所以就不像前3个一样直接用service进行增查改
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        //调用service添加章节
        boolean flag = chapterService.deleteChapter(chapterId);

        //若flag=true代表删除成功，否则删除失败
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

